﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Rally_like_a_madman.Data;
using System.Windows.Navigation;

namespace Rally_like_a_madman
{
    public partial class EndOfRoutePage : PhoneApplicationPage
    {
        public EndOfRoutePage()
        {
            InitializeComponent();
            NavigationHelper.SetEndTime();

            tbRouteName.Text = NavigationHelper.GetCurrentRoute().ToString();
            tbEndTime.Text = NavigationHelper.GetEndTime().ToString("HH:mm:ss");
            tbStartTime.Text = NavigationHelper.GetStartTime().ToString("HH:mm:ss");
            TimeSpan walkedTime = NavigationHelper.GetEndTime() - NavigationHelper.GetStartTime();

            tbWalkedTime.Text = walkedTime.ToString("g");
        }

        private bool addHighscore()
        {
            App thisApp = Application.Current as App;
            TimeSpan walkedTime = NavigationHelper.GetEndTime() - NavigationHelper.GetStartTime();

            User newUser = new User();

            if (tblUserName.Text == string.Empty)
            {
                MessageBox.Show("Voer een geldige naam in.");
                return false;
            }

            newUser.Name = tblUserName.Text;

            thisApp.ActiveDB.UserTable.InsertOnSubmit(newUser);

            Score newScore = new Score();
            newScore.Time = (int) walkedTime.TotalSeconds;
            newScore.Route = NavigationHelper.GetCurrentRoute();
            newScore.User = newUser;

            thisApp.ActiveDB.ScoreTable.InsertOnSubmit(newScore);
            thisApp.ActiveDB.SubmitChanges();

            return true;
        }

        private void btnSubmitHighscore_Click(object sender, RoutedEventArgs e)
        {
            if(addHighscore())
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            while (this.NavigationService.BackStack.Any())
            {
                //According to a post on StackOverflow:
                //RemoveBackEntry sometimes throws NullReferenceException. 
                //Some logic inside navigation system is broken so on FAS it sometimes adds phantom journalentries with PageInstance == null.
                try
                {
                    this.NavigationService.RemoveBackEntry();
                }
                catch (Exception)
                { }
            }

            base.OnNavigatedTo(e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHomeScreen_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult m = MessageBox.Show("Weet je zeker dat je naar het beginscherm wil zonder je highscore op te slaan?",
                "Highscore opslaan?", MessageBoxButton.OKCancel);

            if(m == MessageBoxResult.OK)
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}